<?php 
/**
* school_admin入口
*/
class School_admin_home extends CI_Controller
{
    
    function __construct()
    {
        parent::__construct();
        //登陆状态检测
        if(!$this->session->userdata('r_user_in'))
            redirect('home');
        //权限检测
        if($this->session->userdata('r_role_id') != 4)
            redirect('home');
    }

    /**
     * 管理员的主页面
     * @return [type]
     */
    function index()
    {
        $school_id=$this->session->userdata('r_school_id');
        //准备数据
        $data = array();
        $data['title']='学校管理员';
        $this->load->model('major_model');
        $this->load->model('school_model');
        $this->load->model('teacher_model');
        $this->load->model('student_model');
        //$this->load->model('class_model');
        $this->load->model('logic_class_model');
        $teachers_for_check = $this->teacher_model->get_teachers_for_check_by_school_id($school_id);
        $index = 0;
        foreach ($teachers_for_check as $item) {
            $major = $this->major_model->get_by_id($item['major_id']);
            $teachers_for_check[$index]['major_name']=$major['name'];
            ++$index;
        }
        $data['teachers_for_check']=$teachers_for_check;
        $data['school']=$this->school_model->get_by_id($school_id);
        $data['major_count']=$this->major_model->count_by_school_id($school_id);
        $data['teacher_count']=$this->teacher_model->count_by_school_id($school_id);
        $data['student_count']=$this->student_model->count_by_school_id($school_id);
        //$data['class_count']=$this->class_model->count_by_school_id($school_id);
        $data['logic_class_count']=$this->logic_class_model->count_by_school_id($school_id);
        $this->load->view('school_admin_home/index',$data);
    } 
    //通过教师的申请
    function teacher_y($teacher_id)
    {
        $this->load->model('teacher_model');
        $school_id=$this->session->userdata('r_school_id');
        $teacher = $this->teacher_model->get_by_id($teacher_id);
        if(!$teacher)
            return $this->load->view('error');
        if($teacher['school_id']!=$school_id)
            return $this->load->view('error');
        //将内容写入数据库
        $item = array(
            'pass' => 1,
            );
        if($this->teacher_model->update($teacher_id,$item))
        {
            $this->session->set_flashdata('message','操作成功');
            redirect('school_admin_home/index');  
        }
        else
        {
            $this->session->set_flashdata('message','操作失败');
            redirect('school_admin_home/index');  
        }
    }
    //拒绝教师的申请
    function teacher_n($teacher_id)
    {
        $this->load->model('teacher_model');
        $this->load->model('user_model');
        $school_id=$this->session->userdata('r_school_id');
        $teacher = $this->teacher_model->get_by_id($teacher_id);
        if(!$teacher)
            return $this->load->view('error');
        if($teacher['school_id']!=$school_id)
            return $this->load->view('error');
        $user = $this->user_model->get_by_id($teacher['user_id']);
        if(!$user)
            return $this->load->view('error');
        //将教师的注册信息从数据库删除
        if($this->teacher_model->delete($teacher_id))
        {
            if($this->user_model->delete($teacher['user_id']))
            {
                $this->session->set_flashdata('message','操作成功');
                redirect('school_admin_home/index');
            }
            else
            {
                $this->session->set_flashdata('message','操作失败');
                redirect('school_admin_home/index');  
            }
        }
        else
        {
            $this->session->set_flashdata('message','操作失败');
            redirect('school_admin_home/index');  
        }
    }

    /**
     * 退出，删除session中的数据
     * @return [type]
     */
    function logout()
    {
        $this->session->sess_destroy();
        redirect('home');
    }
    
    /**
     * 修改密码
     * @return [type] [description]
     */
    function change_password()
    {
        //表单验证
        if($this->form_validation->run('school_admin_home/change_password'))
        {
            //post方法
            //读取数据
            $old_password=$this->input->post('old_password');
            $new_password=$this->input->post('new_password');
            
            $username=$this->session->userdata('r_username');
            $this->load->model('user_model');
            $user=$this->user_model->check_user_valid($username,$old_password);
            if($user)
            {
                $user['password']=md5($new_password);
                if($this->user_model->update($user['id'],$user))
                {
                    $this->session->set_flashdata('message','修改成功');
                    redirect('school_admin_home/index');
                }
                else
                {
                    $this->session->set_flashdata('message','操作失败');
                    redirect('school_admin_home/index');
                }
                    
            }
            else{
                $this->session->set_flashdata('message','密码错误，修改失败');
                redirect('admin_home/index');   
            }
                        
        }

        //get方法
        $data['title']='修改密码';
        $this->load->view('school_admin_home/change_password',$data);
    }
}
 